﻿using UnityEngine.Networking;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace ZyGame.Transport
{
    public enum UploadState
    {
        Successfuly,
        Failurefuly,
    }

    class UploadApi
    {
        private const string create = "avatar/resource/v1/matter/create/";
        private const string delete = "avatar/resource/v1/matter/delete/";
        private const string done = "avatar/resource/v1/matter/done?name=#name";


        private static UnityWebRequest PostJson(string url, string uid, string pid, string token, string json)
        {
            UnityWebRequest request = new UnityWebRequest(url, "POST");
            request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
            request.SetRequestHeader("Authorization", token);
            request.SetRequestHeader("userid", uid);
            request.SetRequestHeader("pid", pid);
            request.certificateHandler = new WebReqSkipCert();
            request.downloadHandler = new DownloadHandlerBuffer();
            request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
            return request;
        }

        //创建文件
        public static UnityWebRequest RequestCreate(UploadConfig config, string fileName, string md5, int size)
        {
            var data = new
            {
                name = fileName,
                md5 = md5,
                size = size,
                type = "application/octet-stream",
            };
            string url = config.url + UploadApi.create;
            return PostJson(url, config.name, config.pid, config.token, JsonConvert.SerializeObject(data));
        }

        //删除文件
        public static UnityWebRequest RequestDelete(UploadConfig config, string md, string fileName)
        {
            var data = new
            {
                sid = config.sid,
                pid = config.pid,
                name = fileName,
                md5 = md
            };
            string url = config.url + UploadApi.delete;
            return PostJson(url, config.name, config.pid, config.token, JsonConvert.SerializeObject(data));
        }

        //上传文件
        public static UnityWebRequest RequestUpload(string content, byte[] fileBytes)
        {
            try
            {
                var map = JObject.Parse(content);
                if (!map.ContainsKey("data")) return null;
                var requestData = map["data"];
                string link = requestData["up_link"].ToString();
                var headers = requestData["headers"];
                UnityWebRequest uploaderRequest = UnityWebRequest.Put(link, fileBytes);
                uploaderRequest.certificateHandler = new WebReqSkipCert();
                foreach (var v in headers)
                {
                    var jp = v.ToObject<JProperty>();

                    string value = jp.Value[0].ToString();
                    string key = jp.Name;
                    uploaderRequest.SetRequestHeader(key, value);
                }
                Client.Console.WriteLine(link);
                return uploaderRequest;
            }
            catch (System.Exception e)
            {
                Client.Console.WriteError(e);
            }
            return default;
        }

        //通知文件上传成功
        public static UnityWebRequest RequestDone(UploadConfig config, string fileName)
        {
            return PostJson(config.url + UploadApi.done.Replace("#name", fileName), config.name, config.pid, config.token, "");
        }
    }

}